define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var STATUS = pubjs.config("CONST/MONITOR_STATUS");
	var REMOVED = 3;

	var List = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-adminSmartMonitorList',
				'isSubGrid': false,
				'grid_config': null, // 列表配置
				'gridName': 'monitor',
				'view_model':{
					'addButton': pubjs.checkRight('monitor.addAlarm'),
					'setEmailButton': true
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			// var el = self.getDOM();

			self.append($([
				'<div class="M-formHead">',
					'<span ms-visible="view.addButton" class="ml10" pub-name="add" pub-mod="@base/common/drop.add" ></span>',
					'<span ms-visible="view.setEmailButton" class="ml10" pub-name="setEmails" pub-mod="@base/common/drop.add" ></span>',
					'<span class="ml10" pub-name="monitorNotice" pub-mod="@base/common/drop.add" ></span>',
				'</div>',
				'<div class="cl" pub-name="highGrid" pub-mod="admin/smartMonitor/grid/grid.monitorList"/>'
			].join('')));

			var config = {
				'highGrid': util.extend(c.grid_config, {
					'hasBatch': true,
					'eventDataLoad': true,
					'hasSearch': true
				}),
				'add':{
					'def': LANG('新建监控设置'),
					'href': '#admin/smartMonitor/edit',
					'href_target': '_self'
				},
				'monitorNotice':{
					'def': LANG('查看监控消息'),
					'href': '#admin/messageMonitor',
					'href_target': '_self'
				},
				'setEmails':{
					'def': LANG('设置提醒邮箱'),
					'useCallback': true
				}
			};

			self.buildFromTemplate(config);

			self.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'items': [
					//版本
					{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('状态：'),
						'data': [
							{'id': STATUS.ALL, 'name': LANG('所有')},
							{'id': STATUS.RUNNING, 'name': LANG('开启')},
							{'id': STATUS.PAUSED, 'name': LANG('暂停')},
							{'id': REMOVED, 'name': LANG('归档')}
						],
						'value': 0,
						'compareValue': true
					}},
					{'name': 'type', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('维度：'),
						'data': [
							{'id': 0, 'name': LANG('所有')},
							{'id': 1, 'name': LANG('推广组')},
							{'id': 2, 'name': LANG('广告位')}
						],
						'value': 0,
						'compareValue': true
					}},
					{'name': 'method', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('操作类型：'),
						'data': [
							{'id': 0, 'name': LANG('所有')},
							//{'id': 1, 'name': LANG('发系统消息')},
							{'id': 2, 'name': LANG('发邮件')},
							{'id': 3, 'name': LANG('推广组暂停')},
							//{'id': 4, 'name': LANG('广告位拉黑')},
						],
						'value': 0,
						'compareValue': true
					}},
					{'name': 'AdvertiserId', 'uri':'modules/sideBarBase.aderGridGroup','config':{
						'url': 'qualification/list',
						'title': LANG('广告主：'),
					}},
				],
				'height': 510
			});
		},
		// 操作列点击事件
		onOperateMenuShow: function(ev) {
			var data = ev.param;
			var status = data.Status;
			var returnList = [];

			// pubjs.checkRight('');

			// 归档状态下，还原操作
			if(data.IsDeleted){
				if(pubjs.checkRight('monitor.restoreAlarm')){
					returnList.push({
						'id':'restore',
						'name':LANG('还原')
					});
				}
				ev.returnValue = returnList;
				return false;
			}

/*			if(!data.IsGranted){
				if(pubjs.checkRight('monitor.grant')){
					returnList.push({
						'id':'auth',
						'name':LANG('授权')
					});
				}
			}*/

			if(data.IsGranted){
				if(pubjs.checkRight('monitor.infoAlarm')){
					returnList.push({
						'id': 'check',
						'name': LANG('查看')
					});
				}
			}else{
				if(pubjs.checkRight('monitor.updateAlarm')){
					returnList.push({
						'id': 'edit',
						'name': LANG('编辑')
					});
				}
			}

			if(pubjs.checkRight('monitor.updateAlarm')){
				returnList.push({
					'id': 'saveas',
					'name': LANG('另存为')
				});
			}


			//if(!data.IsGranted){
				if(pubjs.checkRight('monitor.updateAlarmStatus')){
					//1 启动  0暂停
					switch (status){
						case STATUS.PAUSED:
							returnList.push({'id': 'start','name': LANG('开启')});
							break;
						case STATUS.RUNNING:
							returnList.push({'id': 'pause','name': LANG('暂停')});
							break;
					}
				}
				if(pubjs.checkRight('monitor.deleteAlarm')){
					returnList.push({'id': 'remove','name': LANG('删除')});
				}
			//}

			ev.returnValue = returnList;
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function(ev) {
			var self = this;
			var key = ev.param[0]['key'];
			var selected = ev.param[1];
			var id = +selected['_id'];

			switch (key){
				case 'check':
					pubjs.controller.navigate('admin/smartMonitor/check/' + id);
					break;
				case 'edit':
					pubjs.controller.navigate('admin/smartMonitor/edit/' + id);
					break;
				case 'saveas':
					pubjs.controller.navigate('admin/smartMonitor/saveas/' + id);
					break;
				case 'auth':
					self.authPopwin(id, selected);
					break;
				case 'start':
				case 'pause':
					self.toggleStatus(id, key);
					break;
				case 'remove':
					self.remove(id, selected);
					break;
				case 'restore':
					self.restore(id, selected);
					break;
			}

		},
		//
		onBatchShow: function(ev){
			var list = [];

			var batch = [
				{id: 'start', name: LANG('开启'), right: 'plan.start'},
				{id: 'pause', name: LANG('暂停'), right: 'plan.pause'},
				{id: 'selectAder', name: LANG('广告主选择'), right: ''},
			];

			// 检查权限
			util.each(batch, function(item){
				if(!item.right || pubjs.checkRight(item.right))
				{
					list.push(item);
				}
			});

			ev.returnValue = list;
			return false;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev){
			var data = ev.param[0];
			var ids = ev.param[1];
			var contents = ev.param[2];
			var key = data.key || '';

			if(!ids || !ids.length) {
				pubjs.alert(LANG('请先选择监控'));
				return false;
			}

			// 设置子菜单的部分内容编辑,id是用点分割
			var keys = key.split('.');
			if (keys.shift() === 'batchEdit') {
				this.$.batchEdit.reset().show().setTitle(data.name).setType(keys.shift()).setParam(ids);
				return false;
			}

			var self = this;
			switch(key){
				case 'start':
					pubjs.mc.send('monitor/start',{'id': ids }, self.onBatchEditSuccess.bind(self));
					break;
				case 'pause':
					pubjs.mc.send('monitor/pause',{'id': ids }, self.onBatchEditSuccess.bind(self));
					break;
			}

			return false;
		},
		onBatchEditSuccess: function(err,data){
			this.$.highGrid.reload();
			return false;
		},
		//设置弹出邮箱的按钮
		onInputClick: function(ev){
			pubjs.mc.send('monitor/getEmails',this.showEmailsPop.bind(this));
		},
		showEmailsPop: function(err,data){
			var self = this;
			var mod = self.get('emailsPop');
			if(!mod){
				self.createAsync('emailsPop','admin/smartMonitor/popwin/popwin.emailsPop',function(mod){
					mod.show().setData(data);
				});
			}
			else{
				mod.show().setData(data);
			}
		},
		//响应右边侧边栏选项
		onSidebarFilter: function(ev){
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;

				switch (type) {

					case 'status':
						if(id != REMOVED){
							param.Status = +id;
							param.IsDeleted = 0;
						}else{
							param.Status = 0;
							param.IsDeleted = 1;
						}
						break;
					case 'type':
						param.DimType = +id;
						break;
					case 'method':
						param.Method = +id;
						break;
					case 'AdvertiserId':
						param.AdvertiserId = item.data;
						break;
				}
			});
			self.$.highGrid.reload(param);
		},
		//发票弹窗
		authPopwin: function(id, data){
			var self = this;
			var mod = self.$.authPopwin;
			if(!mod){
				self.createAsync('authPopwin', 'admin/smartMonitor/popwin/popwin.authPop',
					{},
					function(module){
						if(module){
							module.setData(data).show();
						}
					}
				)
			}else{
				mod.setData(data).show();
			}

		},
		//禁止和启动操作
		toggleStatus: function(id, key){
			var self = this;
			var statusText = '';
			var status = STATUS.PAUSED;
			switch (key){
				case 'start':
					statusText = LANG('开启');
					status = STATUS.RUNNING;
					break;
				case 'pause':
					statusText = LANG('暂停');
					status = STATUS.PAUSED;
					break;
			}

			pubjs.confirm(LANG('%1该监控设置？', statusText),
				function(isOk){
					if(isOk) {
						pubjs.mc.send('monitor/updateAlarmStatus', {
								'_id': id,
								'Status': status
							},
							self.onToggleStatus.bind(self)
						);
					}
				}
			);
		},
		onToggleStatus: function(){
			// pubjs.alert('修改成功');
			this.reload();
			return false;
		},
		//删除操作
		remove: function(id, data){
			if(!id){
				return false;
			}
			var self = this;
			pubjs.confirm(LANG('是否确认删除？'),function(isOk){
				if(isOk){
					pubjs.mc.send('monitor/deleteAlarm',{'_id': id},
						self.onRemove.bind(self)
					);
				}
			});
		},
		onRemove: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert("还原成功");
			this.reload();
		},
		restore: function(id, data){
			if(!id){
				return false;
			}
			var self = this;
			pubjs.confirm(LANG('是否确认还原该监控？'),function(isOk){
				if(isOk){
					pubjs.mc.send('monitor/restoreAlarm',{'_id': id},
						self.onRemove.bind(self)
					);
				}
			});
		},
		reload: function(param) {
			this.$.highGrid.reload(param);
		},
		onAddAuthSuccess: function(){
			this.reload();
		}
	});
	exports.list = List;
});